package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityGoodsCategoryDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategory;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategory.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategoryExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategoryExample.Criteria;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityGoodsCategoryManager {
	
    @Autowired
    private EstateProjectActivityGoodsCategoryDAO dao;
    
    public EstateProjectActivityGoodsCategory getEstateProjectActivityGoodsCategory(Integer activityCategoryId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(activityCategoryId, EstateProjectActivityGoodsCategory.NOT_DELETED);
	}

	public Pagination<EstateProjectActivityGoodsCategory> queryEstateProjectActivityGoodsCategory(Integer projectId,String activityName,Boolean online, Integer level, Sorting[] sorts, Page page) {
		EstateProjectActivityGoodsCategoryExample example = new EstateProjectActivityGoodsCategoryExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNullOrEmpty(activityName)) {
			criteria.andActivityNameEqualTo(activityName);
		}
		if(Check.notNull(online)) {
			criteria.andOnlineEqualTo(online);
		}
		if(Check.notNull(level)) {
			criteria.andLevelEqualTo(level);
		}
		criteria.andProjectIdEqualTo(projectId).andDeletedEqualTo(EstateProjectActivityGoodsCategory.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public List<EstateProjectActivityGoodsCategory> childrenCategory(Integer activityCategoryId){
		EstateProjectActivityGoodsCategoryExample example = new EstateProjectActivityGoodsCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentCategoryIdEqualTo(activityCategoryId).andDeletedEqualTo(EstateProjectActivityGoodsCategory.NOT_DELETED);
		return dao.selectByExample(example);
	}
	
	public EstateProjectActivityGoodsCategory getEstateProjectActivityGoodsCategoryOnline(Integer activityCategoryId) {
		EstateProjectActivityGoodsCategoryExample example = new EstateProjectActivityGoodsCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andActivityCategoryIdEqualTo(activityCategoryId).andDeletedEqualTo(EstateProjectActivityGoodsCategory.NOT_DELETED).andOnlineEqualTo(true);
		return dao.selectOneByExample(example);
	}

	public Pagination<EstateProjectActivityGoodsCategory> queryEstateProjectActivityGoodsCategoryOnline(Integer parentCategoryId, Page page) {
		EstateProjectActivityGoodsCategoryExample example = new EstateProjectActivityGoodsCategoryExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNullAndGtZero(parentCategoryId)) {
			criteria.andParentCategoryIdEqualTo(parentCategoryId);
		}
		criteria.andDeletedEqualTo(EstateProjectActivityGoodsCategory.NOT_DELETED).andOnlineEqualTo(true);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.weight.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	public EstateProjectActivityGoodsCategory addEstateProjectActivityGoodsCategory(EstateProjectActivityGoodsCategory record, String createUser) {
		record.setDeleted(EstateProjectActivityGoodsCategory.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
	}

	public EstateProjectActivityGoodsCategory editEstateProjectActivityGoodsCategory(EstateProjectActivityGoodsCategory record, String updateUser) {
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
		return record;
	}

	public void delEstateProjectActivityGoodsCategory(EstateProjectActivityGoodsCategory record, String updateUser) {
		EstateProjectActivityGoodsCategoryExample example = new EstateProjectActivityGoodsCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentCategoryIdEqualTo(record.getActivityCategoryId()).andDeletedEqualTo(EstateProjectActivityGoodsCategory.NOT_DELETED);
		long childrenCount = dao.countByExample(example);
		BizAssert.isTrue(childrenCount < 1,ResultCode.BIZ.build(1), "存在{}个子分类，无法删除",childrenCount);
		record.setDeleted(EstateProjectActivityGoodsCategory.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
	}

	public void onlineEstateProjectActivityGoodsCategory(EstateProjectActivityGoodsCategory record, String updateUser) {
		record.setOnline(true);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}

	public void offlineEstateProjectActivityGoodsCategory(EstateProjectActivityGoodsCategory record, String updateUser) {
		record.setOnline(false);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}
}